﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using WebAPITest.Models.Database;
using WebAPITest.service;

namespace WebAPITest.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AuthenticationController : ControllerBase
    {
        //注入jwt服务
        private readonly WebEnterpriseIIContext _db;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="db"></param>
        private readonly IJWTService _jwtService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="jwtService"></param>
        public readonly IUserService _userService;
        
        public AuthenticationController(IJWTService jwtService, WebEnterpriseIIContext db,IUserService userService)
        {
            _jwtService = jwtService;
            _db = db;
            _userService = userService;
        }
        /// <summary>
        /// 创建token
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string CreateToken(string name, int userId, string email, string nickname)
        {
            return _jwtService.CreateToken(name,userId,email,nickname);
        }
        [HttpGet]
        [Authorize]
        public void Test()
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        //public string UserInfo()
        //{
        //    //获取用户信息
        //    return Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value;
        //}
        public object UserInfo()
        {
            //获取用户信息
            //数据来源于Token用户信息保存
            //return new
            //{
            //    //Email = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email).Value,
            //    //UserNmae = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value,
            //    //NickName = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type =="NickName").Value,
            //    //UserId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value,

            //};
            //通过userId查询数据库数据
            var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var user = _db.Users.FirstOrDefault(x => x.UserId == Convert.ToInt32(userId));
            return user;
        }
        //区别假如token之后 修改了数据库数据 修改了用户昵称 邮箱这些数据
        //从数据库里查询出来的数据 token的数据是不变化d
        //在重新登录之前Token保存的用户数据不会变化 而数据库数据还是实时变化的


        /// <summary>
        /// 完成注册功能
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string AddUser( string UserName, string Password, byte Salt, string Email, string NickName, byte UserLevel,User request)
        {
            
            //判断用户名是否存在
            if (_userService.ExistUserByUserName(request.UserName))
            {
                return "名字已存在";
            }
            //var model = new User();
            //model.UserName = UserName;
            //model.Password = Password;
            //model.Salt = Salt;
            //model.Email = Email;
            //model.NickName = NickName;
            //model.UserLevel = UserLevel;
            //model.CreateTime = DateTime.Now;
            //_db.Users.Add(model);
            //var row = _db.SaveChanges();
            var userId = _userService.AddUserByUser(request.UserName, request.Password, request.Salt, request.Email, request.NickName, request.UserLevel);
            if (userId>0)
            {
                return "注册成功！";
            }
            return "注册失败！";
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string Login(string username, string password)
        {
            //第一步 从数据库查询用户为username的用户
            //如果不存在用户就不存在
            //如果存在 判断查询出来的用户密码是不是与password相等
            //如果不相等 返回密码不正确
            //如果相等
            //创建Token返回token给前端
           // var user = _db.Users.FirstOrDefault(x => x.UserName == username);
           var user=_userService.GetUserByUserName(username);
            if (user == null)
            {
                return "用户不存在";
            }
            if (user.Password != password)
            {
                return "密码不正确";
            }
            return _jwtService.CreateToken(user.UserName,user.UserId,user.Email,user.NickName);
        }
    }
}
